package com.example.yckjbigdataflink.utils;

import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 调用
 *     KerberosUtil.login("/etc/krb5.conf", "/path/to/keytab", "principal@REALM");
 *     KerberosTicketRenewal.startRenewal();
 */
public class KerberosTicketRenewal {

    private static final String keytab = "/path/to/your.keytab";
    private static final String principal = "your_principal@YOUR.REALM";

    public static void startRenewal() {
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            try {
                UserGroupInformation.loginUserFromKeytab(principal, keytab);
                System.out.println("Kerberos ticket renewed at " + System.currentTimeMillis());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, 0, 60, TimeUnit.MINUTES); // 每60分钟续期一次
    }
}

